Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
You can have the ListView control automatically sort the items it displays. The control has three properties that determine how the sorting is done: • Set Sorted to True or False to determine whether the list is sorted or not. • Set SortOrder to lvwAscending (value = 0, the default) or to lvwDescending (value = 1) for an ascending or descending sort order. • Set SortKey to 0 (the default) to sort based on the values in the ListItem objects’ Text property. Set SortKey to a value of 1 or greater to sort on the subitem with the specified Index. The second collection that the ListView control uses is ColumnHeaders. As you can probably guess by now, this collection contains ColumnHeader objects. Each item in this collection corresponds to a column of data displayed in the ListView control and, of course, is relevant only when the control is in Report view. You can add to this collection at design time, using the control’s property pages, or at runtime with the Add method. The syntax is quite similar to the Add method for the other collections we have dealt with:
    ListView1.ColumnHeaders.Add index, key, text, width, alignment
The index argument specifies the position of the new ColumnHeader in the collection. If this argument is omitted, the new entry is placed at the end of the collection. Because the columns are displayed across the top of the ListView control in numerical order (left to right), you can use the index argument to determine display order. The key argument is a unique string that is used to refer to the object in code, and the text argument is the text that is displayed in the column heading. Width specifies the width of the column using the scale units of the container that the ListView control is placed in. You can omit the width argument, in which case the default value of 1,440 twips (1 inch) is used. Alignment specifies how both the column header text and the data in the column are aligned. Possible values are lvwColumnLeft (value 0, the default), lvwColumnRight (value 1), and lvwColumnCenter (value 2). I am sure that you can figure out the meanings of these constants. Be aware that the first column must be left-aligned, and if you try to set a different alignment, an error occurs. The following code will add three column headers to a ListView control, each with a width of 1,000 twips:
    ListView1.ColumnHeaders.Add 1, , “Last Name”, 1000
    ListView1.ColumnHeaders.Add 2, , “First Name”, 1000
    ListView1.ColumnHeaders.Add 3, , “Middle Initial”, 1000
Once you have set up your columns, how do you add the details that will be displayed there? Each ListItem object in the ListView control has a SubItems property that consists of an array of strings. This array holds the detail data that is displayed when the ListView control is in Report view mode. This is a 1-based array; the first element holds the detail item to be displayed in the first extra column (the first, or left-most column displays the text stored in the ListItem’s Text property). Note that the ColumnHeader with index 2 refers to the column containing the detail data in SubItems(1). The detail information must be added at runtime, as shown here:
    Dim lvi As ListItem
    Set lvi = ListView1.ListItems.Add(, “Aitken”, “Aitken”)
    lvi.SubItems(1) = “Peter”
    lvi.SubItems(2) = “G.”

    ListView1.ListItems.Add , “Clinton”, “Clinton”
    Set lvi = ListView1.ListItems(“Clinton”)
    lvi.SubItems(1) = “William”
    lvi.SubItems(2) = “J.”

    ListView1.ListItems.Add , “Edison”, “Edison”
    ListView1.ListItems(“Edison”).SubItems(1) = “Thomas”
    ListView1.ListItems(“Edison”).SubItems(2) = “A.”
You can see that I used three different methods of achieving the same thing—adding a ListItem and setting two of its SubItems. All of these approaches are exactly alike and demonstrate the flexibility of the Visual Basic collections model. After executing the two code fragments, the ListView control will appear as in Figure 7.4. Of course, the control mus have its Viewproperty set to 3-lvwReport for this code to work. User access to items displayed in a ListView control is provided by means of the SelectedItem property. This property returns a reference to the ListItem object that is currently selected—that is, highlighted. For example, place the following code in a ListView control’s Click event procedure to display a message box identifying the list item clicked on by the user: Figure 7.4 The ListView control after adding three items to it.
    Private Sub ListView1_Click()

    Dim item As ListItem

    Set item = ListView1.SelectedItem
    MsgBox (item.Text)

    End Sub
You can also use the SelectedItem property to specify programmatically which item in the list will be highlighted. The following code will highlight the second item in the list:
    Set ListView1.SelectedItem = ListView1.ListItems(2)
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.